package yjpay.index.web.control;

import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.util.encrypt.AES;
import com.util.encrypt.RSA;

@Controller
public class CaculateWebCtrl {
	/**
	 * 访问地址http://localhost:8080/payapidemo/
	 * 
	 * @return
	 */

	@RequestMapping(value = "/caculate/to_rsa")
	public String rsa() {
		return "rsa.jsp";
	}

	@RequestMapping(value = "/caculate/rsa")
	public ModelAndView merchantQueryOrder(Model model) throws Exception {

		// 生成RSA密钥对
		Map<String, String> m = RSA.generateKeyPair();
		String publickey = m.get("publicKey");
		String privatekey = m.get("privateKey");

		System.out.println("publickey:" + publickey);
		System.out.println("privatekey:" + privatekey);

		ModelAndView modelAndView = new ModelAndView();
		modelAndView.addObject("publickey", publickey);
		modelAndView.addObject("privatekey", privatekey);

		modelAndView.setViewName("rsa.jsp");
		return modelAndView;
	}

	@RequestMapping(value = "/caculate/to_aes")
	public ModelAndView aes() {
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.addObject("strSec", "yeepay.com");
		modelAndView.setViewName("aes.jsp");
		return modelAndView;
	}

	@RequestMapping(value = "/caculate/aes")
	public ModelAndView aes(@RequestParam("strSec") String strSec, Model model)
			throws Exception {
		String aeskey = "1234567890123456";

		System.out.println(aeskey);
		// 加密
		String data = AES.encryptToBase64(strSec, aeskey);
		// 解密
		String de = AES.decryptFromBase64(data, aeskey);

		ModelAndView modelAndView = new ModelAndView();
		modelAndView.addObject("strSec", strSec);
		modelAndView.addObject("secrectstring", data);
		modelAndView.addObject("desecret", de);

		modelAndView.setViewName("aes.jsp");
		return modelAndView;
	}
	

}
